<template>
  <div >
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 19px">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>品牌列表</el-breadcrumb-item>
    </el-breadcrumb>
  <el-divider></el-divider>
    <el-table :data="tableData" border style="width: 100%;">
      <el-table-column prop="id" label="id" width="100px" align="center"></el-table-column>
      <el-table-column prop="name" label="名称" width="120" align="center"></el-table-column>
      <el-table-column prop="pinyin" label="拼音" width="100px" header-align="center"></el-table-column>
      <el-table-column prop="logo" label="LOGO" width="50" header-align="center"></el-table-column>
      <el-table-column prop="description" label="简介" width="150" header-align="center"></el-table-column>
      <el-table-column prop="keywords" label="关键字" width="100" header-align="center"></el-table-column>
      <el-table-column prop="sort" label="自定义排序" width="50px"></el-table-column>
      <el-table-column prop="sales" label="销量" width="100px"></el-table-column>
      <el-table-column prop="productCount" label="商品种类总和" width="100px"></el-table-column>
      <el-table-column prop="commentCount" label="买家评论数量总和" width="100px"></el-table-column>
      <el-table-column prop="positiveCommentCount" label="买家好评数量总和" width="100px"></el-table-column>
      <el-table-column prop="enable" label="是否启用" width="100px">
        <template slot-scope="scope">
          <el-switch
              v-model="scope.row.enable"
              @change="updateEnable(scope.row)"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#aaaa">
          </el-switch>
        </template>
      </el-table-column>

      <el-table-column  label="操作" width="150" header-align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini" @click="handleEdit(scope.row)"></el-button>
          <el-button type="danger" @click="openDeleteConfirm(scope.row)" icon="el-icon-delete" circle size="mini" ></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
export default {
  data() {
    return {
      tableData:[]
    }
  },
  methods: {
    updateEnable(brand){
      let brandText =['禁用','启用'];
      let url = 'http://localhost:9090/brands/'+brand.id;
      if(brand.enable == 1){
        url = url+'/enable';
      }else {
        url = url+'/disable';
      }
      this.axios.post(url).then((response) =>{
        let responseBody = response.data;
        if(responseBody.state==20000){
          let message='将品牌【'+brand.name+'】已经【'+brandText[brand.enable]+'】成功';
          this.$message({
            showClose: true,
            message: message,
            type: 'success'
          });
        }else {
          this.$message.error(responseBody.message)
        }
        if(responseBody.state == 40400){
          this.loadBrandList();
        }

      })

    },

    handleEdit(brand){
      let message= '您正在尝试编辑【' + brand.id + '-' + brand.name + '】的品牌详情，改你大爷的改';
      this.$alert(message,'提示',{
        confirmButtonText:'确定'
      });
    },
    handleDelete(brand){
      let url = 'http://localhost:9090/brands/'+brand.id+'/delete'
      this.axios.post(url).then((response) =>{
        let responseBody = response.data;
        if(responseBody.state != 20000){
          this.$message.error(responseBody.message)
        }
        if(responseBody.state == 40400|| responseBody.state==20000){
          this.loadBrandList();
        }
      })
      console.log('删除的id='+ brand.id)

    },
    openDeleteConfirm(brand) {
      this.$confirm('此操作将永久删除【'+brand.name+'】品牌, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(brand)
      }).catch(() => {});
    },

    loadBrandList(){
      let url = "http://localhost:9090/brands";
      console.log('url=' + url);
      this.axios.get(url).then((response) =>{
        let responseBody = response.data;
        this.tableData = responseBody.data;
      })
    },


  },
  mounted() {
    console.log("mounted")
    this.loadBrandList();
  }

}
</script>